package com.example.tms.controller;

import com.example.tms.bean.ReturnInfo;
import com.example.tms.bean.UserInfo;
import com.example.tms.bean.UserView;
import com.example.tms.mapper.UserMapper;
import com.example.tms.mapper.UserVMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录退出
 */
@RestController
public class LogController {


    private final UserMapper userMapper;




    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public LogController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    //登录
    @RequestMapping(value = "/loginUser",method = RequestMethod.POST)
    public ReturnInfo userLogin(@RequestBody UserInfo userInfo){

       if(checkSfzh(userInfo.getYhm())){
           userInfo.setYhm(userInfo.getYhm().toUpperCase());
       }
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(userInfo.getYhm(),userInfo.getMima());
        Subject subject = SecurityUtils.getSubject();
        String b="登录失败";
        try {
            UserInfo userInfo1 = userMapper.findByYhm(userInfo.getYhm());
            subject.login(usernamePasswordToken);
            SecurityUtils.getSubject().getSession().setTimeout(-1000);
            if(userInfo1.getShzt().equals("审核通过")) {
                logger.info("用户登录成功！ 用户名：" + userInfo.getYhm() + " 密码：" + userInfo.getMima());
                b = "登录成功！";
            }else if (userInfo1.getShzt().equals("审核不通过")){
                b = "注册审核不通过，不通过的原因为："+ userInfo1.getYuanyin();
            }else if (userInfo1.getShzt().equals("未审核")){
                b="正在审核中，请耐心等待！";
            }
        }catch (Exception e){
            b = "帐号密码错误！";
        }
        return ReturnInfo.info1(b);
    }

    //退出
    @RequestMapping(value = "/logoutUser",method = RequestMethod.POST)
    public void userLogout(){
        Subject subject = SecurityUtils.getSubject();
        UserView userView = (UserView)subject.getPrincipal();
        System.out.println(userView.toString());
        logger.info("用户退出成功！ 用户名:"+userView.getYhm());
        subject.logout();
    }

    //登录app
//    @RequestMapping(value = "/applogin",method = RequestMethod.POST)
//    public ReturnInfo userLogin(@RequestBody DriverApp driverApp) {
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(driverApp.getSfzh(), driverApp.getMima());
//        Subject subject = SecurityUtils.getSubject();
//        String b="登录失败";
//        if (driverAMapper.findBySfzh(driverApp.getSfzh(), true) == null) {
//            b ="用户不存在";
//
//        } else {
//            DriverApp driverApp1 = driverAMapper.findBySfzh(driverApp.getSfzh(), true);
//            if (driverApp.getMima().equals(driverApp1.getMima())){
//                if (driverApp1.getShzt().equals("审核通过")) {
//                    subject.login(usernamePasswordToken);
//                    SecurityUtils.getSubject().getSession().setTimeout(-1000);
//                    logger.info("用户登录成功！ 用户名：" + driverApp.getSfzh() + " 密码：" + driverApp.getMima());
//                    b="登录成功！";
//                } else if (driverApp1.getShzt().equals("审核不通过")) {
//                    b="注册审核不通过，不通过的原因为："+ driverApp1.getYuanyin();
//                } else if (driverApp1.getShzt().equals("未审核")) {
//                    b="正在审核中，请耐心等待！";
//                }
//            }else {
//                b="密码错误！";
//            }
//        }
//        return ReturnInfo.info1(b);
//
//
//    }

    //检查身份证号格式
    private Boolean checkSfzh(String sfzh) {
        Boolean b = false;
        if(sfzh.matches("^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$")){
            b = true;
        }
        return b;
    }
}
